#ifndef ERRORQUEUE_H
#define ERRORQUEUE_H

#include "common.h"
#include <queue>
template <typename T> class ErrorItem
{
	T m_nReturnValue;
public:
	ErrorItem(int value)
	{
		m_nReturnValue = value;
	}
	ErrorItem& operator =(T value)
	{
		m_nReturnValue = value;
		return *this;
	}
	operator int()
	{
		return m_nReturnValue;
	}
};

class ErrorQueue
{
	typedef deque<ErrorItem<int> > Queue;
	Queue m_cItem;
	bool m_nSuccesIndicator;
	bool m_bFireException;
	int m_nMaximumSize;
public:
	ErrorQueue(int maximumSize = 100):m_nSuccesIndicator(true),m_bFireException(false),m_nMaximumSize(maximumSize)
	{};
	ErrorQueue& operator=(int value)
	{
		if (value != 0)
			if (m_bFireException)
				throw giboException("Exception in errorQueue: ",value);
			else
				m_nSuccesIndicator = false;
		if (m_cItem.size() >= m_nMaximumSize)
			m_cItem.pop_back();
		m_cItem.push_front(value);
		return *this;
	}
	operator bool()
	{
		return m_nSuccesIndicator;
	}
	bool operator !()
	{
		return !m_nSuccesIndicator;
	}

	void clear()
	{
		m_cItem.clear();
		m_nSuccesIndicator = true;
	}
	int getFirstError()
	{
		int pos = 0;
		for(Queue::reverse_iterator it = m_cItem.rbegin() ; it != m_cItem.rend(); it++)
		{
			++pos; 
			if (*it != 0) break;
		}
		pos = m_cItem.size() - pos;
		return pos;
	}
	void setFireException(bool fire = true)
	{
		m_bFireException = fire;
	}

	bool getStatus()
	{
		return m_nSuccesIndicator;
	}
	int operator[](int index)
	{
		return m_cItem.at(index);
	}
};





#endif